package jsyf.oa.service.report;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import jsyf.com.util.DateUtils;
import jsyf.com.util.FastJsonConvert;
import jsyf.oa.dao.system.CompanyDao;
import jsyf.oa.dao.system.RegionDao;
import jsyf.oa.dao.wreport.WReportCusVisitDao;
import jsyf.oa.facade.report.WrtVisitReportFacade;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2018/1/26 0026.
 */
@Service
@com.alibaba.dubbo.config.annotation.Service(protocol = {"dubbo"})
public class WrtVisitReportService implements WrtVisitReportFacade {
    @Autowired
    private WReportCusVisitDao wReportCusVisitDao;
    @Autowired
    private RegionDao regionDao;
    @Autowired
    private CompanyDao companyDao;
    private static String one = "1";
    private static String two = "2";

    @Override
    public List<JSONObject> getById(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String userId = jsonParams.getString("userId");
        return regionDao.getById(userId);
    }


    @Override
    public JSONObject getByUserId(String jsonData) {
        JSONObject result = new JSONObject();
        JSONObject todays=new JSONObject();
        JSON months= new JSONObject();
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String userId = jsonParams.getString("userId");
        String companyId = jsonParams.getString("companyId");
        String type = jsonParams.getString("type");
        Date dayCurrent = wReportCusVisitDao.getCurrentTime();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String dateTime = sdf.format(dayCurrent);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(dayCurrent);
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        String isApp = jsonParams.getString("isApp");
        String yesterday = getYesterDay();
        if (dateTime.length() == 10) {
            dateTime= getIsApp(isApp, dateTime, yesterday);
        }
        if (one.equals(type)){
            if (StringUtils.isNotBlank(companyId)){
                todays=wReportCusVisitDao.getByAllCompanyId(companyId,dateTime,yesterday);
            }
        }
        else if (two.equals(type)){
            if (StringUtils.isNotBlank(companyId)){
                todays=wReportCusVisitDao.getByCompanyId(companyId,dateTime,yesterday);
            }
        }else {
            if (StringUtils.isNotBlank(userId)){
                todays=wReportCusVisitDao.getByUserId(userId,dateTime,yesterday);
            }
        }
        SimpleDateFormat sdfDay = new SimpleDateFormat("yyyy-MM");
        Date currentTime = wReportCusVisitDao.getCurrentTime();
        String month=sdfDay.format(currentTime);
        if (one.equals(type)){
            if (StringUtils.isNotBlank(companyId)){
                months=wReportCusVisitDao.getByAllCompanyId(companyId,month,"");
            }
        }
        else if (two.equals(type)){
            if (StringUtils.isNotBlank(companyId)){
                months=wReportCusVisitDao.getByCompanyId(companyId,month,"");
            }
        }else {
            if (StringUtils.isNotBlank(userId)){
                months=wReportCusVisitDao.getByUserId(userId,month,"");
            }
        }
        result.put("today", todays);
        result.put("month", months);
        return result;
    }


    @Override
    public List<JSONObject> getRegionByUserId(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String dateTime = jsonParams.getString("dateTime");
        String departId = jsonParams.getString("departId");
        String type = jsonParams.getString("type");
        String target = "";
        if ("1".equals(type)) {
            target = "FACE_TARGET";
        } else {
            target = "TEL_TARGET";
        }
        String isApp = jsonParams.getString("isApp");
        String yesterday = getYesterDay();
        if (dateTime.length() == 10) {
            dateTime= getIsApp(isApp, dateTime, yesterday);
        }
        //列表
        return regionDao.getRegionByUserId(target, Integer.valueOf(type), dateTime, yesterday, departId);

    }

    @Override
    public JSONObject getRegionByManageChart(String jsonData) {
        JSONObject results = new JSONObject();
        List<String> regionName = new ArrayList<>();
        List<Integer> faceValue = new ArrayList<>();
        List<Integer> telValue = new ArrayList<>();
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String dateTime = jsonParams.getString("dateTime");
        String companyId = jsonParams.getString("companyId");
        String isApp = jsonParams.getString("isApp");
        String yesterday = getYesterDay();
        if (dateTime.length() == 10) {
            dateTime= getIsApp(isApp, dateTime, yesterday);
        }
        List<JSONObject> chart = regionDao.getRegionByManageChart(dateTime, yesterday, companyId);
        for (int i = 0; i < chart.size(); i++) {
            JSONObject faces = chart.get(i);
            regionName.add(faces.getString("managerName"));
        }
        for (int i = 0; i < chart.size(); i++) {
            JSONObject tels = chart.get(i);
            telValue.add(Integer.valueOf(tels.getString("telComplete")));
        }
        for (int i = 0; i < chart.size(); i++) {
            JSONObject faces = chart.get(i);
            faceValue.add(Integer.valueOf(faces.getString("faceComplete")));
        }
        results.put("regionName", regionName);
        results.put("faceValue", faceValue);
        results.put("telValue", telValue);
        return results;
    }

    @Override
    public List<JSONObject> getRegionByManage(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String dateTime = jsonParams.getString("dateTime");
        String companyId = jsonParams.getString("companyId");
        String type = jsonParams.getString("type");
        String isApp = jsonParams.getString("isApp");
        String yesterday = getYesterDay();
        if (dateTime.length() == 10) {
            dateTime= getIsApp(isApp, dateTime, yesterday);
        }
        String target = "";
        if ("1".equals(type)) {
            target = "FACE_TARGET";
        } else {
            target = "TEL_TARGET";
        }
        //列表
        return regionDao.getRegionByManage(target, Integer.valueOf(type), dateTime, yesterday, companyId);
    }

    @Override
    public List<JSONObject> getBigRegionByManager(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String dateTime = jsonParams.getString("dateTime");
        String companyId = jsonParams.getString("companyId");
        String type = jsonParams.getString("type");
        String isApp = jsonParams.getString("isApp");
        String yesterday = getYesterDay();
        if (dateTime.length() == 10) {
            dateTime= getIsApp(isApp, dateTime, yesterday);
        }
        String target = "";
        if ("1".equals(type)) {
            target = "FACE_TARGET";
        } else {
            target = "TEL_TARGET";
        }
        //列表
        return regionDao.getBigRegionByManager(target, Integer.valueOf(type), dateTime, yesterday, companyId);
    }

    @Override
    public JSONObject getRegionByChart(String jsonData) {
        JSONObject results = new JSONObject();
        List<String> regionName = new ArrayList<>();
        List<Double> faceValue = new ArrayList<>();
        List<Double> telValue = new ArrayList<>();
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String dateTime = jsonParams.getString("dateTime");
        String companyId = jsonParams.getString("companyId");
        String isApp = jsonParams.getString("isApp");
        String yesterday = getYesterDay();
        if (dateTime.length() == 10) {
            dateTime= getIsApp(isApp, dateTime, yesterday);
        }
        List<JSONObject> chart = regionDao.getRegionByChart(dateTime, yesterday, companyId);
        for (int i = 0; i < chart.size(); i++) {
            JSONObject faces = chart.get(i);
            regionName.add(faces.getString("regionName"));
        }
        for (int i = 0; i < chart.size(); i++) {
            JSONObject tels = chart.get(i);
            telValue.add(Double.parseDouble(tels.getString("averageTelComplete")));
        }
        for (int i = 0; i < chart.size(); i++) {
            JSONObject faces = chart.get(i);
            faceValue.add(Double.parseDouble(faces.getString("averageFaceComplete")));
        }
        results.put("regionName", regionName);
        results.put("faceValue", faceValue);
        results.put("telValue", telValue);
        return results;
    }

    @Override
    public JSONObject getRegionInfo(String jsonData) {
        JSONObject results = new JSONObject();
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String companyId = jsonParams.getString("companyId");
        String dateTime = jsonParams.getString("dateTime");
        String isApp = jsonParams.getString("isApp");
        String yesterday = "";
        if (dateTime.length() == 10) {
            yesterday = getYesterDay();
        }
        if (dateTime.length() == 10) {
            dateTime= getIsApp(isApp, dateTime, yesterday);
        }
        JSONObject faceInfo = wReportCusVisitDao.getByFaceInfo(dateTime, companyId);
        JSONObject telInfo = wReportCusVisitDao.getByTelInfo(dateTime, yesterday, companyId);
        results.put("faceInfo", faceInfo);
        results.put("telInfo", telInfo);
        return results;
    }


    @Override
    public List<JSONObject> getCompanyByManage(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String dateTime = jsonParams.getString("dateTime");
        String type = jsonParams.getString("type");
        String isApp = jsonParams.getString("isApp");
        String target = "";
        if ("1".equals(type)) {
            target = "FACE_TARGET";
        } else {
            target = "TEL_TARGET";
        }
        String yesterday = getYesterDay();
        if (dateTime.length() == 10) {
           dateTime= getIsApp(isApp, dateTime, yesterday);
        }
        return companyDao.getCompanyByManage(target, Integer.valueOf(type), dateTime, yesterday);
    }

    private String getIsApp(String isApp, String dateTime, String yesterday) {
        Calendar calendar = Calendar.getInstance();
        if (StringUtils.isNotBlank(isApp) && isApp.equals("1")) {
            boolean flag = DateUtils.isInDate(calendar.getTime(), "00:00:00", "08:00:00");
            if (flag) {
                dateTime = yesterday;
                return dateTime;
            }else {
                return dateTime;
            }
        }else {
            boolean flag = DateUtils.isInDate(calendar.getTime(), "00:00:00", "12:00:00");
            if (flag) {
                dateTime = yesterday;
                return dateTime;
            }
            return dateTime;
        }
    }

    @Override
    public JSONObject getCompanyByChart(String jsonData) {
        JSONObject results = new JSONObject();
        List<String> companyName = new ArrayList<>();
        List<Double> faceValue = new ArrayList<>();
        List<Double> telValue = new ArrayList<>();
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String dateTime = jsonParams.getString("dateTime");
        String isApp = jsonParams.getString("isApp");
        String yesterday = getYesterDay();
        if (dateTime.length() == 10) {
            dateTime= getIsApp(isApp, dateTime, yesterday);
        }
        List<JSONObject> chart = companyDao.getCompanyByChart(dateTime, yesterday);
        for (int i = 0; i < chart.size(); i++) {
            JSONObject faces = chart.get(i);
            companyName.add(faces.getString("companyName"));
        }
        for (int i = 0; i < chart.size(); i++) {
            JSONObject tels = chart.get(i);
            telValue.add(Double.parseDouble(tels.getString("averageTelComplete")));
        }
        for (int i = 0; i < chart.size(); i++) {
            JSONObject faces = chart.get(i);
            faceValue.add(Double.parseDouble(faces.getString("averageFaceComplete")));
        }
        results.put("companyName", companyName);
        results.put("faceValue", faceValue);
        results.put("telValue", telValue);
        return results;
    }

    @Override
    public JSONObject getPeopleByUserId(String jsonData) {
        JSONObject results = new JSONObject();
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String userId = jsonParams.getString("userId");
        String time = jsonParams.getString("dateTime");
        String isApp = jsonParams.getString("isApp");
        Date dayCurrent = null;
        String yesterday = "";
        if (time.length() == 10) {
            try {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                Calendar calendar = Calendar.getInstance();
                dayCurrent = sdf.parse(time);
                calendar.setTime(dayCurrent);
                calendar.add(Calendar.DAY_OF_MONTH, -1);
                dayCurrent = calendar.getTime();
                yesterday = sdf.format(dayCurrent);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }

        if (time.length() == 10) {
            time= getIsApp(isApp, time, yesterday);
        }
        List<JSONObject> faceTargetDay = regionDao.getPeopleByUserId(userId, time, yesterday, 1);
        JSONObject faceTargetInfo = regionDao.getPeopleInfoByUserId(userId, time, yesterday, 1);
        List<JSONObject> telTargetDay = regionDao.getPeopleByUserId(userId, time, yesterday, 0);
        JSONObject telTargetInfo = regionDao.getPeopleInfoByUserId(userId, time, yesterday, 0);
        results.put("faceTargetInfo", faceTargetInfo);
        results.put("faceTargetDay", faceTargetDay);
        results.put("telTargetDay", telTargetDay);
        results.put("telTargeInfo", telTargetInfo);
        return results;
    }


    public String getYesterDay() {
        //通过传过来的时间算出前一天
        Date dayCurrent = wReportCusVisitDao.getCurrentTime();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(dayCurrent);
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        dayCurrent = calendar.getTime();
        return sdf.format(dayCurrent);
    }


}
